package controller

import (
	"article_vote/config"
	"article_vote/logic"
	"github.com/gin-gonic/gin"
)

type IndexController struct {}

func (ctrl *IndexController) Index(ctx *gin.Context) {
	redisClient, _ := config.GetRedis("default")
	redisClient.Set("test", "aaa", 0)
	ctx.JSON(200, gin.H{"msg":"set key success"})
}

func (ctrl *IndexController) GetKey(ctx *gin.Context)  {
	redisClient, _ := config.GetRedis("default")
	val, _:= redisClient.Get("test").Result()
	ctx.JSON(200, gin.H{"msg":val})
}

func (ctrl *IndexController) GetArticleList(ctx *gin.Context)  {
	ids := []int{1,3,5}
	model := &logic.ArticleLogic{}
	list := model.GetArticleList(ids)
	ctx.JSON(200, gin.H{"data":list})
}

func (ctrl *IndexController) GetAtricleListForRedis(ctx *gin.Context)  {
	redisClient, _ := config.GetRedis("default")
	res, _ := redisClient.ZRangeWithScores("score:", 0, -1).Result()
	ctx.JSON(200, gin.H{"data":res})
}

func (ctrl *IndexController) CreateAuthor(ctx *gin.Context) {
	name := ctx.PostForm("name")

	authorLogic := &logic.AuthorLogic{}
	authorLogic.Create(name)
	ctx.JSON(200, gin.H{"msg":"保存成功"})
}